package leetcode.biweekly.week96;

import java.util.Arrays;
import java.util.TreeMap;

//Solution3Test
public class Solution5 {
	public long maxScore(int[] nums1, int[] nums2, int k) {
		int n = nums1.length;
		int[][] nums = new int[n][2];
		for (int i = 0; i < n; i++) {
			nums[i][0] = nums1[i];
			nums[i][1] = nums2[i];
		}
		Arrays.sort(nums, (a, b) -> b[0] - a[0]);
		TreeMap<Integer, Integer> tm = new TreeMap<>();
		int p = 0;
		long[] sums = new long[(int) 1e5 + 1];
		long sum = 0;
		while (p < k) {
			int[] cur = nums[p];
			int n1 = cur[0], n2 = cur[1];
			tm.put(n2, tm.getOrDefault(n2, 0) + 1);
			sums[n2] += n1;
			sum += n1;
			p++;
		}
		long ans = 1l * sum * tm.firstKey();
		while (p < n) {
			int min = tm.firstKey();
			int minsize = tm.get(min);
			sum -= sums[min];
		}
		return ans;
	}

}
